package com.open.exam.timedtask;

import com.open.exam.businessservice.TimedTaskBusinessService;
import com.open.exam.lock.AcquiredLockWorker;
import com.open.exam.lock.impl.RedisLocker;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * @Description 超时自动交卷
 * @author 奶茶
 * Created by on 2019/2/20 15:05.
 */
@Component
public class SubmitPaperByOvertime {

    private static final Logger logger = LoggerFactory.getLogger(SubmitPaperByOvertime.class);

    @Autowired
    private TimedTaskBusinessService timedTaskBusinessService;
    @Autowired
    private RedisLocker redisLocker;

    /**
     * 超时自动交卷
     * 每天12点执行 0 0 0 * * ?
     * 每五分钟执行 0 0/5 * * * ?
     * @throws InterruptedException
     */
    @Scheduled(cron = "0 0 0 * * ?")
    public void timedSubmitUNSubmitPaper() throws InterruptedException {
        logger.info("---------------------------自动交卷开始---------------------------");
        redisLocker.lock("SUBMIT_PAPER", 90, 1, new AcquiredLockWorker<Object>() {
            @Override
            public Object invokeAfterLockAcquired() throws Exception {
                return timedTaskBusinessService.submitPaperByOvertime();
            }
        });
        logger.info("---------------------------自动交卷结束---------------------------");
    }

}
